<?php
global $_GPC, $_W;
$uniacid = $_W['uniacid'];
$id = $_GPC['id'];
$yid = $_GPC['yid'];
$res = $this->res;
$plus = $this->plus;
load()->func('tpl');
load()->model('mc');
$setting = $this->baseset($uniacid);
$userinfo = $this->Get_checkoauth();
$openid = $_W['openid'];
if (empty($_W['openid'])) {
	$openid = $userinfo['openid'];
}
$avatar = $userinfo['avatar'];
$nickname = $userinfo['nickname'];
$nickname = $this->emoji($nickname);
if (empty($openid)) {
	$this->goindex();
}
$follow = $userinfo['follow'];
$user = $this->getuserinfo($openid, $userinfo['nickname'], $userinfo['avatar'], $follow);
$yuser = pdo_fetch("SELECT * FROM " . tablename($this->tableuser) . " WHERE uniacid = '{$_W['uniacid']}' AND id = '{$yid}'");
$yaoqing = pdo_fetch("SELECT * FROM " . tablename($this->tableyaoqing) . " WHERE uniacid = '{$_W['uniacid']}' AND openid = '{$openid}'");
if (empty($yuser)) {
	message('邀请人信息不存在！', $this->createMobileUrl('index'), 'error');
}
if ($user) {
	message('您已是会员，无法被邀请！', $this->createMobileUrl('index'), 'error');
} else {
	$data = array('uniacid' => $uniacid, 'openid' => $openid, 'nickname' => $nickname, 'avatar' => $avatar, 'ctime' => TIMESTAMP, 'vtime' => TIMESTAMP,);
	pdo_insert($this->tableuser, $data);
	if (empty($yaoqing)) {
		$jifen = $setting['yaoqingjifen'];
		$ydata = array('uniacid' => $uniacid, 'openid' => $openid, 'nickname' => $nickname, 'avatar' => $avatar, 'yid' => $yuser['id'], 'yopenid' => $yuser['openid'], 'jifen' => $jifen, 'ctime' => TIMESTAMP, 'vtime' => TIMESTAMP,);
		pdo_insert($this->tableyaoqing, $ydata);
		load()->model('mc');
		$uid = mc_openid2uid($yuser['openid']);
		mc_credit_update($uid, 'credit1', $jifen, array(0, '成功邀请' . $nickname . ',奖励【' . $jifen . '】积分'));
	} else {
		$this->goindex();
	}
	message('您已成功接受好友的邀请', $this->createMobileUrl('index'), 'success');
}